home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BPNN133U.ZIP / RANDOM.C < prev    next >
C/C++ Source or Header  |  1992-11-19  |  3KB  |  124 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:        random.c
  4. *    desc:        routine for a very-long-cycle random-number sequences
  5. *    by:        patrick ko shu pui
  6. *    date:        6 sep 1991
  7. *    comment:
  8. *
  9. *    this random number generator was proved to generate random sequences
  10. *    between 0 to 1 which if 100 numbers were calculated every second, it
  11. *    would NOT repeat itself for over 220 years.
  12. *
  13. *    reference:
  14. *
  15. *    Wichmann B.A. and I.D. Hill. "Building A Random Number Generator."
  16. *    Byte Magazine. March 1987. pp.127.
  17. *
  18. *    remark:        this C routine is a freeware
  19. *
  20. *    ko053@cucs19.cs.cuhk.hk    Internet 
  21. *    BiG Programming Club (since 1987), Hong Kong, 6:700/132 FidoNet
  22. *    [852] 654-8751
  23. *-----------------------------------------------------------------------------
  24. */
  25. #include    <time.h>
  26. #include    <values.h>
  27.  
  28. #include    "random.h"
  29.  
  30. #define    REAL    double
  31. #define    INT    int
  32.  
  33. /*
  34. *    default seed values
  35. */
  36. static INT    x = 1;
  37. static INT    y = 10000;
  38. static INT    z = 3000;
  39.  
  40. /*
  41. *=============================================================================
  42. *    funct:        rndmize
  43. *    dscpt:        generating random number seeds
  44. *    given:        nothing
  45. *    retrn:        nothing
  46. *=============================================================================
  47. */
  48. INT rndmize()
  49. {
  50.     time_t    timer;
  51.  
  52.     x = time( &timer ) % MAXINT;
  53.     y = (x * x) % MAXINT;
  54.     z = (y * y) % MAXINT;
  55. }
  56.  
  57. /*
  58. *=============================================================================
  59. *    funct:        rnd
  60. *    dscpt:        return a random number of range 0-1
  61. *    given:        nothing
  62. *    retrn:        the random number
  63. *    cmmnt:        you may use prandomize to change the seeds
  64. *=============================================================================
  65. */
  66. REAL rnd()
  67. {
  68.     REAL    temp;
  69.  
  70.     /*
  71.     *    first generator
  72.     */
  73.     x = 171 * (x % 177) - 2 * (x / 177);
  74.     if (x < 0)
  75.         {
  76.         x += 30269;
  77.         }
  78.  
  79.     /*
  80.     *    second generator
  81.     */
  82.     y = 172 * (y % 176) - 35 * (y / 176);
  83.     if (y < 0)
  84.         {
  85.         y += 30307;
  86.         }
  87.  
  88.     /*
  89.     *    third generator
  90.     */
  91.     z = 170 * (z % 178) - 63 * (z / 178);
  92.     if (z < 0)
  93.         {
  94.         z += 30323;
  95.         }
  96.  
  97.     /*
  98.     *    combine to give function value
  99.     */
  100.     temp = x/30269.0 + y/30307.0 + z/30323.0;
  101.  
  102.     return (temp - (INT)temp);
  103. }
  104.  
  105.  
  106. /*
  107. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  108. * * *                E X A M P L E
  109. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  110. */
  111. /*
  112. int main()
  113. {
  114.     int     i;
  115.  
  116.     rndmize();
  117.  
  118.     for (i=0; i<100; i++)
  119.         {
  120.         printf("%f,", rnd() );
  121.         }
  122. }
  123. */
  124.